package cn.zhz.customview.view.record;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;

import java.util.ArrayList;
import java.util.Arrays;

import androidx.annotation.Nullable;
import cn.zhz.customview.R;
import cn.zhz.customview.Utils;

/**
 * @project: customViewDemo
 * @author: ZhuangHZ
 * @createTime: 2019/6/5 21:39
 * @describe: 录音界面的音律条和图案组合view
 * @version:
 */
public class RecordWaveView extends View {


    private int mWidth, mHeight;//该view的总高度
    private Bitmap icon;
    private float[] resetScales = {0.5f, 0.75f, 1f, 0.75f, 0.5f, 0.25f, 0.5f, 0.75f, 1f};
    private float[] scales = {0.5f, 0.75f, 1f, 0.75f, 0.5f, 0.25f, 0.5f, 0.75f, 1f};
    private ArrayList<ValueAnimator> scaleAnimList = new ArrayList();
    private Paint imgPaint;
    private Paint linePaint;
    private int iconLX, iconRX;//图片左边X坐标和右边X坐标
    private int alpha = 255;
    private float lineWidth = 20f;//旁边线条宽度
    private float lineMargin = 30f; //线的间隔

    private float lineTop = 0f; //线的上方Y坐标
    private float lineBottom = 0f; //线的下方Y坐标
    private boolean isStarted;

    public RecordWaveView(Context context) {
        this(context, null);
    }

    public RecordWaveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecordWaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        imgPaint = new Paint();
        imgPaint.setAntiAlias(true);
        imgPaint.setColor(Color.WHITE);
        imgPaint.setFilterBitmap(true);
        linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setColor(Color.WHITE);
        icon = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_speak_micro);
        mWidth = Utils.getScreenWidth(getContext());
        mHeight = icon.getHeight() + 24;
        iconLX = ((mWidth - icon.getWidth()) / 2);
        iconRX = ((mWidth - icon.getWidth()) / 2) + icon.getWidth();

        lineTop = (mHeight - icon.getHeight()) / 2;
        lineBottom = icon.getHeight() + 12f;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //首先先绘制中间的图标
        drawCenter(canvas);
        drawLeftLine(canvas);
        drawRightLine(canvas);
    }

    private void drawCenter(Canvas canvas) {
        canvas.save();
        canvas.drawBitmap(icon, (getWidth() - icon.getWidth()) / 2f, (mHeight - icon.getHeight()) / 2, imgPaint);
        canvas.restore();
    }


    private void drawLeftLine(Canvas canvas) {
        for (int i = 0; i < scales.length; i++) {
            canvas.save();
            float lineRX = iconLX - (i + 1) * lineMargin - i * lineWidth;
            float lineLX = lineRX - lineWidth;
            float lineYM = (lineTop + lineBottom) / 2;
            float top = lineYM - ((lineBottom - lineTop) / 2 * scales[i]);
            float bottom = lineYM + ((lineBottom - lineTop) / 2 * scales[i]);
            RectF rectF = new RectF(lineLX, top, lineRX, bottom);
            int alphaTmp = alpha - alpha * i / scales.length;
            linePaint.setAlpha(alphaTmp);
            canvas.drawRoundRect(rectF, 10f, 10f, linePaint);
            canvas.restore();
        }
    }

    private void drawRightLine(Canvas canvas) {
        for (int i = 0; i < scales.length; i++) {
            canvas.save();
            float lineLX = iconRX + (i + 1) * lineMargin + i * lineWidth;
            float lineRX = lineLX + lineWidth;
            float lineYM = (lineTop + lineBottom) / 2;
            float top = lineYM - ((lineBottom - lineTop) / 2 * scales[i]);
            float bottom = lineYM + ((lineBottom - lineTop) / 2 * scales[i]);
            RectF rectF = new RectF(lineLX, top, lineRX, bottom);
            int alphaTmp = alpha - alpha * i / scales.length;
            linePaint.setAlpha(alphaTmp);
            canvas.drawRoundRect(rectF, 10f, 10f, linePaint);
            canvas.restore();
        }
    }

    private void startLineAnim() {
        if (!scaleAnimList.isEmpty()) {
            for (ValueAnimator animator : scaleAnimList) {
                if (animator.isPaused()) {
                    animator.resume();
                } else {
                    if (!animator.isStarted()) {
                        animator.start();
                    }
                }
            }
            return;
        }
        float[] floats = {0.5f, 0.75f, 1f, 0.75f, 0.5f, 0.25f,
                0.5f, 0.75f, 1f, 0.75f, 0.5f, 0.25f,
                0.5f, 0.75f, 1f, 0.75f, 0.5f, 0.25f};


        //该方式开销较大，后面修改,相同的复用
        for (int i = 0; i < scales.length; i++) {
            final int tmp = i;
            float[] tmpArray = new float[7];
            System.arraycopy(floats, tmp, tmpArray, 0, 7);
            ValueAnimator scaleAnim = ValueAnimator.ofFloat(tmpArray);
            scaleAnim.setDuration(1000);
            scaleAnim.setRepeatCount(-1);
            scaleAnim.setInterpolator(new LinearInterpolator());
            scaleAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    scales[tmp] = (float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });

            scaleAnim.start();
            scaleAnimList.add(scaleAnim);
        }

    }


    public void startAnim() {
        if (isStarted) {
            return;
        }
        startLineAnim();
        isStarted = true;
    }


    /**
     * 立即停止
     */
    public void stopLineAnimation() {
        if (!isStarted) {
            return;
        }
        for (ValueAnimator animator : scaleAnimList) {
            if (animator.isRunning()) {
                animator.pause();
            }
        }
        isStarted = false;
    }

    /**
     * 还原
     */
    public void resetLineAnimation() {
        for (ValueAnimator animator : scaleAnimList) {
            animator.end();
        }
        isStarted = false;
    }


    public boolean isStarted() {
        return isStarted;
    }

    /**
     * 实现中间图标的点击效果
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if(checkClickPoint(event)){

                }

                break;
        }
        return super.onTouchEvent(event);
    }


    /**
     * 检查点击的点是否在图片里面
     */
    private boolean checkClickPoint(MotionEvent event) {
        if(event.getRawX()>iconLX&&event.getRawX()<iconRX){
            return true;
        }
        return false;
    }

}
